package com.example.note2;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    private Button textbtn;
    private ListView lv;
    private Adapter adapter;
    private NotesDB notesDB;//创建数据库对象
    private SQLiteDatabase dbReader;//获取权限对象
    private Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
    }
    public void initview(){
        lv=findViewById(R.id.list);
        textbtn =findViewById(R.id.xinjian);
        notesDB=new NotesDB(this);
        dbReader=notesDB.getReadableDatabase();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cursor.moveToPosition(position);
                Intent intent=new Intent(MainActivity.this,SelectAct.class);
                intent.putExtra(NotesDB.ID,cursor.getInt(cursor.getColumnIndex(NotesDB.ID)));
                intent.putExtra(NotesDB.CONTENE,cursor.getString(cursor.getColumnIndex(NotesDB.CONTENE)));
                intent.putExtra(NotesDB.TIME,cursor.getString(cursor.getColumnIndex(NotesDB.TIME)));
                startActivity(intent);
            }
        });
        textbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent(MainActivity.this,AddContent.class);
                startActivity(intent);
            }
        });
    }
    public void selectDB(){
        Cursor cursor=dbReader.query(NotesDB.TABLE_NAME,null,null,null,null,null,null);
        adapter=new MyAdapater(this,cursor);
        lv.setAdapter((ListAdapter) adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        selectDB();
    }
}
